#include "window.h"

#include "epsilon.h"

#include "EpsilonInput/keyboard.h"
#include "EpsilonInput/mouse.h"

  /// Default constructor
EPSILON_API
eWindowParameters::eWindowParameters (void)
{
  size    = sizeof (eWindowParameters);
//  version = STRUCT_VER;
  flags   = E_WF_NONE;
}

eWindow::eWindow (void)
{
  fullscreen  = false;

  *title      = _T ('\0');

  wndListener       = NULL;
  wndResizeCallback = NULL;
  wndMoveCallback   = NULL;
}

EPSILON_API
eWindow::~eWindow (void)
{
  fullscreen = false;

  // Cleanup
  Destroy ();
}

EPSILON_API
void
eWindow::Move (const int& left, const int& top)
{
  internals.move (left, top);
}

EPSILON_API
void
eWindow::MoveClient (const int& left, const int& top)
{
  internals.move_client (left, top);
}

EPSILON_API
void
eWindow::Resize (const int& x,    const int& y)
{
  internals.resize (x, y);
}

EPSILON_API
void
eWindow::ResizeClient (const int& x,    const int& y)
{
  internals.resize_client (x, y);
}


EPSILON_API
bool
eWindow::Create (const eWindowParameters& parameters)
{
  _tcsncpy (title, parameters.window_name, 256);

#ifdef WIN32
#  define NATIVE_WIN_FLAG eWindowParameters::E_WF_WIN32
#else
#  ifndef __APPLE__
#    define NATIVE_WIN_FLAG eWindowParameters::E_WF_X11
#  else
#    define NATIVE_WIN_FLAG eWindowParameters::E_WF_QUARTZ
#  endif
#endif

  if (parameters.flags & NATIVE_WIN_FLAG)
    if (internals.create (parameters, this)) {
      return true;
    } else {
      internals.destroy ();
      return false;
    }

  else {
    eSetLastError (EPSILON_ERROR_INVALID_WINDOW_FLAG);
    return false;
  }
}

EPSILON_API
bool
eWindow::Destroy (void)
{
  return internals.destroy ();
}


eWindowInternals::eWindowInternals (void)
{
  initialized = false;

  wndActive   = false;
  wndShown    = false;

  /* Is there any WM where the cursor might be
       invisible until ShowCursor (...) is called?

        - I can think of none, but there _may_ be!
  */
  cursorShown = true;

  keyboard    = NULL;
  mouse       = NULL;

  ZeroMemory (&wndRect, sizeof (wndRect));
}

eWindowInternals::~eWindowInternals (void)
{
  if (keyboard != NULL)
    delete keyboard;

  keyboard = NULL;

  if (mouse != NULL)
    delete mouse;

  mouse = NULL;
}








